//
//  NSDictionary+Extension.m
//  Netease
//
//  Created by 王方伟 on 2017/8/25.
//  Copyright © 2017年 wfw. All rights reserved.
//

#import "NSDictionary+Extension.h"

@implementation NSDictionary (Extension)
- (NSString *)jsonBody{
    NSData *data = [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
    if (data) {
        return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    return nil;
}

- (NSDictionary *)jsonDict: (NSString *)key{
    id object = [self objectForKey:key];
    return [object isKindOfClass:[NSDictionary class]] ? object : nil;
}

- (NSInteger)jsonInteger: (NSString *)key{
    id object = [self objectForKey:key];
    if ([object isKindOfClass:[NSString class]] ||
        [object isKindOfClass:[NSNumber class]]){
        return [object integerValue];
    }
    return 0;
}

- (NSString *)jsonString: (NSString *)key{
    id object = [self objectForKey:key];
    if ([object isKindOfClass:[NSString class]]){
        return object;
    }else if([object isKindOfClass:[NSNumber class]]){
        return [object stringValue];
    }
    return nil;
}
@end
